package com.suneee.refund.dao.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.springframework.stereotype.Repository;

import com.suneee.base.util.ContextHelper;
import com.suneee.context.page.Page;
import com.suneee.context.page.Sort;
import com.suneee.context.page.Sort.Order;
import com.suneee.core.util.StringUtils;
import com.suneee.orm.GenericDao;
import com.suneee.orm.util.WhereClauseBuilder;
import com.suneee.refund.bean.Condition;
import com.suneee.refund.dao.ViewRefundApplyDao;
import com.suneee.refund.entity.ViewRefundApply;
import com.suneee.refund.params.RefundParams;

@Repository("viewRefundApplyDao")
public class ViewRefundApplyDaoImpl extends GenericDao<ViewRefundApply, String>implements ViewRefundApplyDao {

	@Override
	public Page<ViewRefundApply> getPageByCondition(Condition condition, String type) {
		// TODO Auto-generated method stub
		WhereClauseBuilder sbf = new WhereClauseBuilder(" select * from v_refund_apply where 1=1 ");
		this.wrapWhereSQL(sbf, condition, type);
		return this.executeQuery(sbf.getSql(), condition, sbf.getArgs());
	}

	private WhereClauseBuilder wrapWhereSQL(WhereClauseBuilder sbf, Condition condition, String type) 
	{
		
		Object [] args = new Object[1];
		args[0] = ContextHelper.getCurrentLoginInfo().getAdminId();
		List<Map<String,Object>> spadmList = this.queryForList("select shop_id from t_shop_admin where admin_id = ?", args);
		List<Object> objList = null;
		if(null != spadmList && spadmList.size() > 0)
		{
			objList = new ArrayList<Object>();	
			for(Map<String,Object> map :spadmList)
			{
				objList.add(map.get("shop_id"));
			}
		}
		if(null == spadmList || spadmList.size() == 0)
		{
			objList = new ArrayList<Object>();
			objList.add(" ");
		}
		if (RefundParams.TYPE_REFUND.equals(type))
		{
			sbf.equals("after_sale_type", RefundParams.AFTER_SALE_TYPE_REFUND_MONEY);
		}
		else
		{
			sbf.append(" and (after_sale_type = ? or after_sale_type = ?) ",
					new Object[] { RefundParams.AFTER_SALE_TYPE_REFUND_REFUND_GOODS,
							RefundParams.AFTER_SALE_TYPE_REFUND_CHANGE_GOODS });
		}
		sbf.equals("tid", condition.getTid());
		sbf.equals("buyer_nick", condition.getBuyerNick());
		sbf.equals("shop_id", condition.getShopId());
		sbf.in("shop_id", objList);
		sbf.equals("outer_sku_id", condition.getOuterSkuId());
		sbf.contains("title", condition.getTitle());
		sbf.equals("enterprise_code", condition.getEnterpriseCode());
		sbf.append(" and (process_status = ? or process_status = ?) ", 
				new Object[] {RefundParams.PROCESS_STATUS_INIT,
				RefundParams.PROCESS_STATUS_PROCESS});
		return sbf;
	}

	@Override
	public Page<Map<String,Object>> getNotCheckedList(Condition condition) {
		WhereClauseBuilder sbf = new WhereClauseBuilder(" select distinct tid,after_sale_type from v_refund_apply where feedback_status is null ");
		sbf.noEquals("after_sale_type", RefundParams.AFTER_SALE_TYPE_REFUND_MONEY);
		sbf.equals("process_status", RefundParams.PROCESS_STATUS_PROCESS);
		//仓库反馈状态（ACCEPT：允许入库；DENY：拒绝入库；SEND_GOODS：已发货）
		sbf.isNull("feedback_status");
		sbf.equals("tid", condition.getTid());
		sbf.equals("buyer_nick", condition.getBuyerNick());
		sbf.equals("shop_id", condition.getShopId());
		sbf.equals("outer_sku_id", condition.getSkuId());
		sbf.contains("title", condition.getTitle());
		sbf.equals("enterprise_code", condition.getEnterpriseCode());
		sbf.equals("channel_code", condition.getChannelCode());
		return this.queryForList(sbf.getSql(), condition, sbf.getArgs());
	}

	@Override
	public Page<Map<String,Object>> getCheckedList(Condition condition) {
		WhereClauseBuilder sbf = new WhereClauseBuilder(" select distinct tid,after_sale_type from v_refund_apply where feedback_status is not null ");
		sbf.noEquals("after_sale_type", RefundParams.AFTER_SALE_TYPE_REFUND_MONEY);
		//sbf.equals("process_status", RefundParams.PROCESS_STATUS_PROCESS);
		sbf.equals("tid", condition.getTid());
		//仓库反馈状态（ACCEPT：允许入库；DENY：拒绝入库；SEND_GOODS：已发货）
		sbf.isNotNull("feedback_status");
		sbf.equals("buyer_nick", condition.getBuyerNick());
		sbf.equals("shop_id", condition.getShopId());
		sbf.equals("outer_sku_id", condition.getSkuId());
		sbf.contains("title", condition.getTitle());
		sbf.equals("enterprise_code", condition.getEnterpriseCode());
		sbf.equals("channel_code", condition.getChannelCode());
		return this.queryForList(sbf.getSql(), condition, sbf.getArgs());
	}

	@Override
	public Page<Map<String,Object>> getNotRefundApplys(Condition condition, String processStatus) {
		List<Object> list = null;
		Integer adminId = ContextHelper.getCurrentLoginInfo().getAdminId();
		List<Map<String,Object>> shopList = this.queryForList("select shop_id from t_shop_admin where admin_id = ?", adminId);
		if(null != shopList && shopList.size() > 0)
		{
			list = new ArrayList<Object>();
			for(Map<String,Object> map :shopList)
			{
				list.add(map.get("shop_id"));
			}	
		}
		if(null == list || list.size() == 0)
		{
			list = new ArrayList<Object>();
			list.add(" ");
		}
		WhereClauseBuilder sbf = new WhereClauseBuilder(" select distinct tid,after_sale_type from v_refund_apply where 1=1 ");
//		sbf.equals("has_good_return", false);
		sbf.equals("process_status", processStatus);
		sbf.equals("tid", condition.getTid());
		sbf.equals("buyer_nick", condition.getBuyerNick());
		sbf.equals("shop_id", condition.getShopId());
		sbf.in("shop_id", list);
		sbf.equals("outer_sku_id", condition.getSkuId());
		sbf.contains("title", condition.getTitle());
		sbf.equals("after_sale_type",condition.getAfterSaleType());
		sbf.equals("enterprise_code", condition.getEnterpriseCode());
		sbf.equals("refund_result", RefundParams.REFUND_RESULT_INIT);
		//sbf.append("and  is null ()", args);
		sbf.equals("is_notice_refund" ,"1");
		
		return this.queryForList(sbf.getSql(), condition, sbf.getArgs());
	}
	
	@Override
	public Page<Map<String,Object>> getRefundApplys(Condition condition, String processStatus) {
		List<Object> list = null;
		Integer adminId = ContextHelper.getCurrentLoginInfo().getAdminId();
		List<Map<String,Object>> shopList = this.queryForList("select shop_id from t_shop_admin where admin_id = ?", adminId);
		if(null != shopList && shopList.size() > 0)
		{
			list = new ArrayList<Object>();
			for(Map<String,Object> map :shopList)
			{
				list.add(map.get("shop_id"));
			}	
		}
		if(null == list || list.size() == 0)
		{
			list = new ArrayList<Object>();
			list.add(" ");
		}
	
		WhereClauseBuilder sbf = new WhereClauseBuilder(" select distinct tid,after_sale_type from v_refund_apply where 1=1 ");
		sbf.noEquals("refund_result", RefundParams.REFUND_RESULT_INIT);
		sbf.equals("tid", condition.getTid());
		sbf.equals("buyer_nick", condition.getBuyerNick());
		sbf.equals("shop_id", condition.getShopId());
		sbf.in("shop_id", list);
		sbf.equals("outer_sku_id", condition.getSkuId());
		sbf.contains("title", condition.getTitle());
		sbf.equals("after_sale_type",condition.getAfterSaleType());
		sbf.equals("enterprise_code", condition.getEnterpriseCode());
		return this.queryForList(sbf.getSql(), condition, sbf.getArgs());
	}

	@Override
	public Page<Map<String,Object>> getHistoryRefundApplys(Condition condition) {
		List<Object> list = null;
		Integer adminId = ContextHelper.getCurrentLoginInfo().getAdminId();
		List<Map<String,Object>> shopList = this.queryForList("select shop_id from t_shop_admin where admin_id = ?", adminId);
		if(null != shopList && shopList.size() > 0)
		{
			list = new ArrayList<Object>();
			for(Map<String,Object> map :shopList)
			{
				list.add(map.get("shop_id"));
			}	
		}
		if(null == list || list.size() == 0)
		{
			list = new ArrayList<Object>();
			list.add(" ");
		}
		// 售后类型(REFUND_MONEY:退款,REFUND_GOODS:退货,CHANGE_GOODS:换货)
		WhereClauseBuilder sbf = new WhereClauseBuilder(" select distinct tid,after_sale_type from v_refund_apply where 1=1 ");
		sbf.equals("after_sale_type", condition.getAfterSaleType());
		sbf.equals("tid", condition.getTid());
		sbf.equals("buyer_nick", condition.getBuyerNick());
		sbf.equals("shop_id", condition.getShopId());
		sbf.in("shop_id", list);
		sbf.equals("outer_sku_id", condition.getOuterSkuId());
		sbf.contains("title", condition.getTitle());
		sbf.equals("enterprise_code", condition.getEnterpriseCode());
		sbf.append(" and (status = ? or status = ? or status = ?) ", new Object[]{RefundParams.ORDER_STATUS_SELLER_REFUSE_BUYER,RefundParams.ORDER_STATUS_CLOSED,RefundParams.ORDER_STATUS_SUCCESS});
		condition.setPageNumber(condition.getPageNumber() == 0 ? 1 : condition.getPageNumber());
		condition.setPageSize(condition.getPageSize() == 0 ? 10 : condition.getPageSize());
		condition.setSort((condition.getSort() == null || StringUtils.isEmpty(condition.getSort().getOrderByClause()))
				? new Sort("shop_id", Order.ASC) : condition.getSort());
		return this.queryForList(sbf.getSql(), condition, sbf.getArgs());

	}

	@Override
	public Page<Map<String, Object>> getTidPage(Condition condition, String type) {
		// TODO Auto-generated method stub
		WhereClauseBuilder sbf = new WhereClauseBuilder(" select distinct tid,after_sale_type from v_refund_apply where 1=1 ");
		this.wrapWhereSQL(sbf, condition, type);
		return this.queryForList(sbf.getSql(), condition, sbf.getArgs());
	}

	@Override
	public List<ViewRefundApply> getViewRefundApplyByTidAndSkuId(String refundId) {

		String sql = "select * from v_refund_apply where refund_id = ? ";
	//	Object[] args = new Object[] { tid, skuId };
		return this.executeQuery(sql, refundId);
	}

	@Override
	public List<ViewRefundApply> getViewRefundApplyByTid(String tid, String afterSaleType,String outSkuId,String title) {
//		String sql = "select * from v_refund_apply where tid = ? ";
//		Object[] args = null;
//		sql += " and after_sale_type = ? and (process_status = ? or process_status = ?) ";
//		args = new Object[] { tid, type,RefundParams.PROCESS_STATUS_INIT,RefundParams.PROCESS_STATUS_PROCESS};
//		return this.executeQuery(sql, args);
		WhereClauseBuilder build = new WhereClauseBuilder();
		build.equals("tid",tid);
		build.equals("after_sale_type",afterSaleType);
		build.appendWhere(" (process_status = '"+RefundParams.PROCESS_STATUS_INIT+"' or process_status = '"+RefundParams.PROCESS_STATUS_PROCESS+"') ");
		build.equals("outer_sku_id", outSkuId);
		build.contains("title", title);
		return this.selectWhere(build.getSql(), build.getArgs());
		//return this.executeQuery(build.getSql(), build.getArgs());
	}
	@Override
	public List<ViewRefundApply> getViewApplyBytid(String tid, String afterSaleType,String skuId,String title) {
		//String sql = "select * from v_refund_apply "+ "where tid = ? "+ "and after_sale_type = ? "+ "and refund_result != ? "+ "and refund_result is not null ";
		//Object[] args =new Object[] {tid,afterSaleType,RefundParams.REFUND_RESULT_INIT};
		//return this.executeQuery(sql, args);
		WhereClauseBuilder build = new WhereClauseBuilder();
		build.equals("tid",tid);
		build.equals("after_sale_type",afterSaleType);
		build.noEquals("refund_result",RefundParams.REFUND_RESULT_INIT);
		build.isNotNull("refund_result");
		build.equals("outer_sku_id", skuId);
		build.contains("title", title);
		//Object[] args =new Object[] {tid,afterSaleType,RefundParams.ORDER_STATUS_SELLER_REFUSE_BUYER,RefundParams.ORDER_STATUS_CLOSED,RefundParams.ORDER_STATUS_SUCCESS};
		return this.selectWhere(build.getSql(), build.getArgs());
	}

	@Override
	public List<ViewRefundApply> getViewRefundApplyBytidNoRefund(String tid,String afterSaleType,String outSkuId,String title) {
		// TODO Auto-generated method stub
//		String sql = "select * from v_refund_apply where tid = ? and after_sale_type = ? and feedback_status is null and process_status = ? ";
//		Object[] args =new Object[] {tid,afterSaleType,RefundParams.PROCESS_STATUS_PROCESS};
		WhereClauseBuilder build = new WhereClauseBuilder();
		build.equals("tid",tid);
		build.equals("after_sale_type",afterSaleType);
		build.isNull("feedback_status");
		build.equals("process_status",RefundParams.PROCESS_STATUS_PROCESS);
		build.equals("outer_sku_id", outSkuId);
		build.contains("title", title);
		return this.selectWhere(build.getSql(), build.getArgs());
	}

	@Override
	public List<ViewRefundApply> getNotViewRefundApplyByTid(String tid,String afterSaleType,String outSkuId,String title) {
		/*String sql = "select * from v_refund_apply where tid = ? "
				+ "and after_sale_type = ? and process_status = ? "
				+ "and refund_result = ? and is_notice_refund = 1 ";
		Object[] args =new Object[] {tid,afterSaleType,RefundParams.PROCESS_STATUS_PROCESS,RefundParams.REFUND_RESULT_INIT};*/
		WhereClauseBuilder build = new WhereClauseBuilder();
		build.equals("tid",tid);
		build.equals("after_sale_type",afterSaleType);
		build.equals("process_status",RefundParams.PROCESS_STATUS_PROCESS);
		build.equals("refund_result",RefundParams.REFUND_RESULT_INIT);
		build.equals("is_notice_refund","1");
		build.equals("outer_sku_id", outSkuId);
		build.contains("title", title);
		return this.selectWhere(build.getSql(), build.getArgs());
	}

	@Override
	public List<ViewRefundApply> getHistoryRefundApplyBytid(String tid,String afterSaleType,String outSkuId,String title) {
		// TODO Auto-generated method stub
	//	String sql = "select * from v_refund_apply where tid = ? and after_sale_type = ? and (status = ? or status = ? or status = ?)";
		WhereClauseBuilder build = new WhereClauseBuilder();
		build.equals("tid",tid);
		build.equals("after_sale_type",afterSaleType);
		build.appendWhere(" (status = '"+RefundParams.ORDER_STATUS_SELLER_REFUSE_BUYER+"' or status = '"+RefundParams.ORDER_STATUS_CLOSED+"' or status = '"+RefundParams.ORDER_STATUS_SUCCESS+"') ");
		build.equals("outer_sku_id", outSkuId);
		build.contains("title", title);
		//Object[] args =new Object[] {tid,afterSaleType,RefundParams.ORDER_STATUS_SELLER_REFUSE_BUYER,RefundParams.ORDER_STATUS_CLOSED,RefundParams.ORDER_STATUS_SUCCESS};
		;
		return this.selectWhere(build.getSql(), build.getArgs());
	}

	@Override
	public List<ViewRefundApply> getViewRefundApplyBytidHasRefund(String tid,String afterSaleType,String outSkuId,String title) {
		//String sql = "select * from v_refund_apply where tid = ? and after_sale_type = ? and feedback_status is not null and process_status != ? ";
		WhereClauseBuilder builder = new WhereClauseBuilder();
		builder.equals("tid", tid);
		builder.equals("after_sale_type", afterSaleType);
		builder.isNotNull("feedback_status");
		builder.noEquals("process_status",RefundParams.PROCESS_STATUS_INIT);
		builder.equals("outer_sku_id", outSkuId);
		builder.contains("title", title);
		//Object[] args =new Object[] {tid,afterSaleType,RefundParams.PROCESS_STATUS_INIT};
		return this.selectWhere(builder.getSql(), builder.getArgs());
		//return this.executeQuery(builder.getSql(), builder.getArgs());
	}
	
}
